﻿$(document).ready(function () {
	$("#cmbTable").kendoComboBox({
		dataTextField: "Description",
		dataValueField: "ID",
		dataSource: {
			type: "json",
			transport: {
				read: {
					url: "Customer/getTableList",
					data: {
						restaurantID: getCurrReserve().Restaurant.ID
					}
				}
			}
		},
		change: cmbTable_onChange
	});

	var date = new Date();
	
	date.setHours(date.getHours() + 1, 0, 0, 0);

	var $dtpDateTime = $("#dtpDateTime").kendoDateTimePicker({
		parseFormats: ["dd/MM/yyyy", "HH:mm:ss"],
		timeFormat: "HH:mm:ss",
		culture: 'es-AR',
		min: date,
		value: date,
		change: dtpDateTime_onChange,
	}).data("kendoDateTimePicker");

	getCurrReserve().Date = kendo.toString($dtpDateTime.value(), 'dd/MM/yyyy');
	getCurrReserve().Time = kendo.toString($dtpDateTime.value(), 'HH:mm:ss');

	function cmbTable_onChange() {
		getCurrReserve().Restaurant.TableList[0] = this.dataItem(this.select());
	}

	function dtpDateTime_onChange() {
		getCurrReserve().Date = kendo.toString(this.value(), 'dd/MM/yyyy');
		getCurrReserve().Time = kendo.toString(this.value(), 'HH:mm:ss');
	}

	$("#btnSaveReserve").click(function () {
		if (validateForm('#divReserveWindow')) {
			if (!saveReserve()) {
				alert("ERR:00x0087643 No hay disponibilidad para la reserva configurada");
			} else {
				location.reload(true);
			}
		}
	});

	$("#btnValidateReserve").click(function () {
		if (validateForm('#divReserveWindow')) {
			if (validateReserve()) {
				$("#spanValidReserve").css("color", "green");
				$("#spanValidReserve span").text("Hay disponibilidad");
			} else {
				$("#spanValidReserve").css("color", "red");
				$("#spanValidReserve span").text("No hay disponibilidad, pruebe cambiando el dia o el horario");
			}
		}
	});

	$("span[controlToValidate]").hide();

	$("#addReserveWindow").data("kendoWindow").center();
});